package crud

import (
	"os"
	"strings"

	"api/system/helper"
)

var mTempl = `<?php
namespace {{.__NAMESPACE__}};

use laytp\BaseModel;

class {{.__CLASS__}} extends BaseModel
{

    //模型名
    protected $name = '{{.__TABLE__}}';

    //附加属性
    protected $append = [];

    //时间戳字段转换
    protected $type = [
	];

}
`

// 生成控制器
func (g *cGlobal) MakeModel() error {
	inx := strings.LastIndex(g.ModelPath, "\\")
	nName := g.ModelPath[:inx]

	err := os.MkdirAll(nName, os.ModePerm)
	if err != nil {
		return err
	}

	cinx := strings.LastIndex(g.TableName, "_")
	lowerClassName := g.TableName[cinx+1:]
	className := helper.UcFirst(lowerClassName)

	m := map[string]string{
		"__NAMESPACE__": g.ModelNameSpace,
		"__CLASS__":     className,
		"__TABLE__":     g.TableName,
	}

	fileName := g.ModelPath
	err = TemplateExec(fileName, mTempl, m)
	if err != nil {
		return err
	}

	return nil
}
